#include "itemwidget.h"

ItemWidget::ItemWidget(CabPosItem cab_pos_item,  int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str, BaseShowWidget *parent) :
                       BaseShowWidget(parent), m_cab_pos_item(cab_pos_item), m_screen_id(screen_id),
                       m_partition_id(partition_id), m_layout_id(layout_id),
                       m_item_id(item_id), m_cab_pos_item_str(cab_pos_item_str)
{
    //为每一个item注册点击事件回调函数，通过全局观察者发送消息
    GlobalObserver::getGlobalObserver()->attach(loss_select_msg, this, SLOT(setLossSelect()));

    loadAttribute(cab_pos_item);
}

void ItemWidget::loadAttribute(CabPosItem cab_pos_item)
{

    if (cab_pos_item.type == "text")
    {
        if (cab_pos_item.background != "")
        {
            clear();
            QPixmap pixmap(g_pro_data.path + QString("/res/") + cab_pos_item.background);
            setPixmap(pixmap);
            setScaledContents(true);
        }

        QString content = cab_pos_item.data.materials.begin().value().content;

        if (!g_preview)
        {
            //表示正常显示
            setText(content);
        } else
        {
            //表示显示预览
            if (preview_map.contains(content))
                setText(preview_map[content]);
            else
                setText(content);
        }

        QString qss = styleSheet();

        if (cab_pos_item.background_color != "")
            replaceQss(qss, "background-color:", QString("background-color:#%1;").arg(cab_pos_item.background_color));
        else
            replaceQss(qss, "background-color:", "background-color:transparent;");

        if (cab_pos_item.bold)
            replaceQss(qss, "font-weight:", "font-weight:bold;");
        else
            replaceQss(qss, "font-weight:", "");


        if (cab_pos_item.font != "")
            replaceQss(qss, "font-family:", QString("font-family:%1;").arg(cab_pos_item.font));
        else
            replaceQss(qss, "font-family:", "");

        if (cab_pos_item.fore_color != "")
            replaceQss(qss, ";color:", QString(";color:#%1;").arg(cab_pos_item.fore_color));
        else
            replaceQss(qss, ";color:", "");

        if (cab_pos_item.size > 0)
            replaceQss(qss, "font-size:", QString("font-size:%1px;").arg(cab_pos_item.size));


        if (cab_pos_item.italic)
            replaceQss(qss, "font-style:", "font-style:italic;");
        else
            replaceQss(qss, "font-style:", "");

        if (cab_pos_item.align == align_left)
            setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        if (cab_pos_item.align == align_center)
            setAlignment((Qt::AlignCenter));
        if (cab_pos_item.align == align_right)
            setAlignment((Qt::AlignRight | Qt::AlignVCenter));

        setStyleSheet(qss);
    }

    if (cab_pos_item.type == "image")
    {
        setText(cab_pos_item.data.materials.begin().value().content);
    }

    if (cab_pos_item.type == "video")
    {
        setText("video");
    }
    setGeometry(cab_pos_item.x, cab_pos_item.y, cab_pos_item.width, cab_pos_item.height);
}


void ItemWidget::setSelectStyle(bool select)//设置控件选择样式
{
    QString qss = styleSheet();

    if (select)
    {
        replaceQss(qss, "border:", "border:2px solid pink;");
        //replaceQss(qss, "background-color:", "background-color:rgb(225,255,255);");
        GlobalObserver::getGlobalObserver()->notify(item_select_msg, m_screen_id, m_partition_id, m_layout_id, m_item_id, m_cab_pos_item_str);
        parentWidget()->parentWidget()->raise();
        raise();
    } else
    {
        replaceQss(qss, "border:", "border:none;");
        //replaceQss(qss, "background-color:", "background-color:transparent;");
    }
    setStyleSheet(qss);
}

void ItemWidget::setAllChildrenLossSelect()
{
    setLossSelect();
}
